/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evangelion.core.daoImpl;

import com.evangelion.core.dao.GenericDao;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PrePersist;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Kellycasma
 */
@Repository
public class GenericDaoImpl<T extends Serializable> implements GenericDao<T> {

    private EntityManagerFactory emf = null;
    EntityManager em;

    public GenericDaoImpl() {
        emf = Persistence.createEntityManagerFactory("EvangelionPU", System.getProperties());
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @PrePersist
    @Override
    public void save(T objeto) {
        em = getEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            em.merge(objeto);
            tx.commit();
        } catch (Exception ex) {
            System.out.println("ex:" + ex);
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @PrePersist
    @Override
    public void delete(T object) {
        em = getEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            em.remove(object);
            tx.commit();
        } catch (Exception ex) {
            System.out.println("ex:" + ex);
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public T find(T object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<T> findByParam(T object, String parametro, String valor) {
        try {
            em = getEntityManager();
            System.out.println("object.getClass().getName():" + object.getClass().getSimpleName());
            Query q = em.createQuery("SELECT x FROM " + object.getClass().getSimpleName() + " x where x." + parametro + "=:valor");
            q.setParameter("valor", "%" + valor + "%");
            return q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<T> ListActive(T object) {
        try {
            em = getEntityManager();
            System.out.println("object.getClass().getName():" + object.getClass().getSimpleName());
            Query q = em.createQuery("SELECT x FROM " + object.getClass().getSimpleName() + " x");
            System.out.println(".....................");
            return q.getResultList();
        } catch (Exception e) {

            System.out.println("findByParam:...." + e);
            return null;
        }
    }

    public List<T> ListAll(T object) {
        try {
            em = getEntityManager();
            System.out.println("object.getClass().getName():" + object.getClass().getSimpleName());
            Query q = em.createQuery("SELECT x FROM " + object.getClass().getSimpleName() + " x");
            return q.getResultList();
        } catch (Exception e) {

            return null;
        }
    }

    @Override
    public T findObjByParam(Class object, String parametro, String valor) {

        try {
            em = getEntityManager();
            System.out.println("object.getClass().getName():" + object.getSimpleName());
            Query q = em.createQuery("SELECT x FROM " + object.getSimpleName() + " x where x." + parametro + "=:valor");
            q.setParameter("valor", valor);
            return (T) q.getSingleResult();
        } catch (Exception e) {
            return null;
        }

    }

    public List<T> findListByParam(Class object, String parametro, String valor) {
        try {
            em = getEntityManager();
            System.out.println("object.getClass().getName():" + object.getSimpleName());
            Query q = em.createQuery("SELECT x FROM " + object.getSimpleName() + " x where x." + parametro + " like :valor");
            q.setParameter("valor", "%" + valor + "%");
            return q.getResultList();
        } catch (Exception e) {
            return null;
        }

    }
    /*
     * Paginacion
     */

    public List<T> searchByParam(Class object, String valorParametro, int start, int max) {
        em = getEntityManager();
        System.out.println("object.getSimpleName():........................." + object.getSimpleName());
        Query q = em.createQuery("SELECT a FROM " + object.getSimpleName() + " a");

        q.setFirstResult(start);
        q.setMaxResults(max);
        return q.getResultList();
    }
public List<T> searchByParamdesde(Class object, String valorParametro, int desde, int hasta) {
        em = getEntityManager();
        System.out.println("object.getSimpleName():........................." + object.getSimpleName());
        Query q = em.createQuery("SELECT a FROM " + object.getSimpleName() + " a WHERE a.id>=:desde and  a.id<=:hasta");

        q.setParameter("desde", desde);
        q.setParameter("hasta", hasta);
        return q.getResultList();
    }
    public int countPage(Class object, String valor) {
        em = getEntityManager();
        Query q = em.createQuery("SELECT COUNT(a.id) from " + object.getSimpleName() + " a");
        return ((Number) q.getSingleResult()).intValue();
    }
}
